package com.ruoyi.web.controller.module.event;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.event.*;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.event.IEventCleanAreaListInfoService;
import com.ruoyi.system.service.module.event.IEventCleanAreaListService;
import com.ruoyi.system.service.module.property.IPropertyService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 保洁的区域路线Controller
 *
 * @author ruoyi
 * @date 2024-09-24
 */
@Validated
@Api(value = "保洁的区域路线控制器", tags = {"保洁的区域路线管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/areaList")
public class EventCleanAreaListController extends BaseController {

    private final IEventCleanAreaListService eventCleanAreaListService;
	private final IEventCleanAreaListInfoService eventCleanAreaListInfoService;
	private final IPropertyService propertyService;
	private final IEstateService estateService;

    /**
     * 查询保洁的区域路线列表
     */
    @ApiOperation("查询保洁的区域路线列表")
    @PreAuthorize("@ss.hasPermi('module:areaList:list')")
    @GetMapping("/list")
    public TableDataInfo<EventCleanAreaList> list(@Validated EventCleanAreaList eventCleanAreaList) {
		SysUser sysUser = getUser();

		QueryWrapper<EventCleanAreaList> qw = new QueryWrapper<>();
		//用户类型 0 - 系统管理员  1 - 平台管理员 2 - 物业管理员  3 - 小区管理员
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(eventCleanAreaList.getPropertyId() != null){
				qw.eq("property_id",eventCleanAreaList.getPropertyId());
			}
			if(eventCleanAreaList.getEstateId() != null){
				qw.eq("estate_id",eventCleanAreaList.getEstateId());
			}
		}
		if(eventCleanAreaList.getAreaType() != null){
			qw.eq("area_type",eventCleanAreaList.getAreaType());
		}
		TableDataInfo<EventCleanAreaList> td = PageUtils.buildDataInfo(eventCleanAreaListService.page(PageUtils.buildPage(),qw));
		List<EventCleanAreaList> list = td.getRows();
		for (EventCleanAreaList list1:list) {
			if (list1.getPropertyId() != null) {
				list1.setPropertyName(propertyService.findById(list1.getPropertyId()).getPropertyName());
			}
			if (list1.getEstateId() != null) {
				list1.setEstateName(estateService.findById(list1.getEstateId()).getEstateName());
			}
		}
		return td;
    }

    /**
     * 导出保洁的区域路线列表
     */
    @ApiOperation("导出保洁的区域路线列表")
    @PreAuthorize("@ss.hasPermi('module:areaList:export')")
    @Log(title = "保洁的区域路线", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventCleanAreaList> export(@Validated EventCleanAreaList eventCleanAreaList) {
        List<EventCleanAreaList> list = eventCleanAreaListService.list();
        ExcelUtil<EventCleanAreaList> util = new ExcelUtil<EventCleanAreaList>(EventCleanAreaList.class);
        return util.exportExcel(list, "保洁的区域路线");
    }

    /**
     * 获取保洁的区域路线详细信息
     */
    @ApiOperation("获取保洁的区域路线详细信息")
    @PreAuthorize("@ss.hasPermi('module:areaList:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventCleanAreaList> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
		EventCleanAreaList eventCleanAreaList = eventCleanAreaListService.getById(id);
		QueryWrapper<EventCleanAreaListInfo> qw = new QueryWrapper<>();
		qw.eq("area_list_id",eventCleanAreaList.getId());
		List<EventCleanAreaListInfo> list = eventCleanAreaListInfoService.list(qw);
		List<EventCleanAreaListInfo> list1 = new ArrayList<>();
		eventCleanAreaList.setListInfos(list);
		return AjaxResult.success(eventCleanAreaList);
    }

    /**
     * 新增保洁的区域路线
     */
    @ApiOperation("新增保洁的区域路线")
    @PreAuthorize("@ss.hasPermi('module:areaList:add')")
    @Log(title = "保洁的区域路线", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventCleanAreaList eventCleanAreaList) {
		Boolean br = eventCleanAreaListService.save(eventCleanAreaList);
		if (eventCleanAreaList.getListInfos()!=null){
			for (int i=0;i<eventCleanAreaList.getListInfos().size();i++){
				EventCleanAreaListInfo info1 = new EventCleanAreaListInfo();
				info1.setId(null);
				info1.setSort(i);
				info1.setAreaId(eventCleanAreaList.getListInfos().get(i).getAreaId());
				info1.setAreaListId(eventCleanAreaList.getId());
				br = eventCleanAreaListInfoService.save(info1);
			}
		}
		return toAjax(br ? 1 : 0);
    }

    /**
     * 修改保洁的区域路线
     */
    @ApiOperation("修改保洁的区域路线")
    @PreAuthorize("@ss.hasPermi('module:areaList:edit')")
    @Log(title = "保洁的区域路线", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventCleanAreaList eventCleanAreaList) {
		//查询并删除info
		QueryWrapper<EventCleanAreaListInfo> qw = new QueryWrapper<>();
		qw.eq("area_list_id",eventCleanAreaList.getId());
		List<EventCleanAreaListInfo> list = eventCleanAreaListInfoService.list(qw);
		for (EventCleanAreaListInfo listInfo:list){
			eventCleanAreaListInfoService.removeById(listInfo.getId());
		}
		Boolean br = eventCleanAreaListService.updateById(eventCleanAreaList);
		if (eventCleanAreaList.getListInfos()!=null){
			for (int i=0;i<eventCleanAreaList.getListInfos().size();i++){
				EventCleanAreaListInfo info1 = new EventCleanAreaListInfo();
				info1.setId(null);
				info1.setSort(i);
				info1.setAreaId(eventCleanAreaList.getListInfos().get(i).getAreaId());
				info1.setAreaListId(eventCleanAreaList.getId());
				br = eventCleanAreaListInfoService.save(info1);
			}
		}
		return toAjax(br ? 1 : 0);
    }

    /**
     * 删除保洁的区域路线
     */
    @ApiOperation("删除保洁的区域路线")
    @PreAuthorize("@ss.hasPermi('module:areaList:remove')")
    @Log(title = "保洁的区域路线" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
		for (Long id:ids){
			QueryWrapper<EventCleanAreaListInfo> qw = new QueryWrapper<>();
			qw.eq("area_list_id",id);
			List<EventCleanAreaListInfo> list = eventCleanAreaListInfoService.list(qw);
			for (EventCleanAreaListInfo listInfo:list){
				eventCleanAreaListInfoService.removeById(listInfo.getId());
			}
		}
		return toAjax(eventCleanAreaListService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
